home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / p4 / p4-1_2a.lha / p4-1.2a / messages / grid_slave.c < prev    next >
C/C++ Source or Header  |  1992-10-19  |  6KB  |  226 lines

  1. #include "p4.h"
  2.     
  3. #ifndef TRUE
  4. #define BOOL            int
  5. #define TRUE            1
  6. #define FALSE           0
  7. #endif
  8.     
  9. #define ROWS             100
  10. #define COLUMNS             100
  11. #define ROWS_PER_SUB     50
  12. #define COLUMNS_PER_SUB     50
  13.  
  14. #define MASTER          0
  15.     
  16. #define PROCS_PER_COL    (ROWS / ROWS_PER_SUB)
  17. #define PROCS_PER_ROW    (COLUMNS / COLUMNS_PER_SUB)
  18. #define N_PROCS        (PROCS_PER_ROW * PROCS_PER_COL)
  19.  
  20. /* message types */
  21. #define CNTL            0
  22. #define C_BOUNDARY    1
  23. #define R_BOUNDARY    2
  24. #define ANSWER          3
  25.  
  26. /* log event types */
  27. #define SEND            102
  28. #define RECEIVE         101
  29. #define CREATE          100
  30. #define WAIT_MSG        6
  31. #define WORKING         4
  32. #define DONE_WORK       5
  33.  
  34. struct cntl_rec {
  35.     int row;
  36.     int col;
  37.     int upper_neighbor;
  38.     int right_neighbor;
  39.     int lower_neighbor;
  40.     int left_neighbor;
  41.     int iterations;
  42.     double bounded_subgrid[ROWS_PER_SUB+2][COLUMNS_PER_SUB+2];
  43. };
  44.  
  45. struct c_boundary {
  46.     double col[ROWS_PER_SUB];
  47. };
  48.  
  49. struct r_boundary {
  50.     double row[COLUMNS_PER_SUB];
  51. };
  52.  
  53. struct answer_rec {
  54.     double subgrid[ROWS_PER_SUB][COLUMNS_PER_SUB];
  55. };
  56.  
  57.  
  58. /* some compilers require this to be outside slave (too many locals) */
  59. double grid1[ROWS_PER_SUB+2][COLUMNS_PER_SUB+2];
  60. double grid2[ROWS_PER_SUB+2][COLUMNS_PER_SUB+2];
  61.  
  62. slave()
  63. {
  64.     struct cntl_rec *rec1;
  65.     double *current_grid, *next_grid, *temp;
  66.     int master_id, proc_id, msg_type;
  67.     int i, j, n, ln;
  68.     
  69.     master_id = MASTER;
  70.     msg_type = CNTL;
  71.     rec1 = NULL;
  72.     p4_recv(&msg_type,&master_id, &rec1, &ln);
  73.     for (i=0; i < (ROWS_PER_SUB + 2); i++) 
  74.         for (j=0; j < (COLUMNS_PER_SUB + 2); j++)
  75.     {
  76.         grid2[i][j] = rec1->bounded_subgrid[i][j];
  77.         grid1[i][j] = rec1->bounded_subgrid[i][j];
  78.     };
  79.     
  80.     current_grid = (double *) grid1;
  81.     next_grid = (double *) grid2;
  82.     p4_dprintfl(5,"in slave, iterations = %d\n",rec1->iterations);
  83.     for (n=rec1->iterations, i=0; i < n; i++)
  84.     {
  85.     /*
  86.     if ((rec1->row == 1) && (rec1->col == 1))
  87.         printf("current subgrid at %d,%d, after %d iterations\n",
  88.            rec1->row,rec1->col,i);
  89.         printsubgrid(current_grid,ROWS_PER_SUB,COLUMNS_PER_SUB);
  90.     */
  91.     compute1_iter(current_grid,next_grid);
  92.     if (!((i + 1) == n))   /* if not the last iteration */
  93.     {
  94.         if (!upper_bound(rec1->row))
  95.         send_row(next_grid, 1, rec1->upper_neighbor);
  96.         if (!lower_bound(rec1->row))
  97.         send_row(next_grid, ROWS_PER_SUB, rec1->lower_neighbor);
  98.         if (!left_bound(rec1->col))
  99.         send_col(next_grid, 1, rec1->left_neighbor);
  100.         if (!right_bound(rec1->col))
  101.         send_col(next_grid, COLUMNS_PER_SUB, rec1->right_neighbor);
  102.         if (!lower_bound(rec1->row))
  103.         receive_row(next_grid, (ROWS_PER_SUB+1),rec1->lower_neighbor);
  104.         if (!upper_bound(rec1->row))
  105.         receive_row(next_grid, 0, rec1->upper_neighbor);
  106.         if (!right_bound(rec1->col))
  107.         receive_col(next_grid,(COLUMNS_PER_SUB+1),rec1->right_neighbor);
  108.         if (!left_bound(rec1->col))
  109.         receive_col(next_grid, 0, rec1->left_neighbor);
  110.     }
  111.     /* swap grids */
  112.     temp = current_grid;
  113.     current_grid = next_grid;
  114.     next_grid = temp;
  115.     }
  116.     send_answer(current_grid, master_id);
  117. }  
  118.  
  119. send_row(grid, row, proc_id)
  120. double grid[ROWS_PER_SUB+2][COLUMNS_PER_SUB+2];
  121. int row, proc_id;
  122. {
  123.     int x, ln, msg_type;
  124.     struct r_boundary m1;
  125.     
  126.     msg_type = R_BOUNDARY;
  127.     for (x=1; x <= COLUMNS_PER_SUB; x++)
  128.     m1.row[x-1] = grid[row][x];
  129.     ln = sizeof(struct r_boundary);
  130.     p4_send(msg_type, proc_id, &m1, ln);
  131. }
  132.  
  133. send_col(grid, col, proc_id)
  134. double grid[ROWS_PER_SUB+2][COLUMNS_PER_SUB+2];
  135. int col;
  136. int proc_id;
  137. {
  138.     struct c_boundary m1;
  139.     int i, ln, msg_type;
  140.     
  141.     msg_type = C_BOUNDARY;
  142.     ln = sizeof(struct c_boundary);
  143.     for (i=1; i <= ROWS_PER_SUB; i++)
  144.     m1.col[i-1] = grid[i][col];
  145.     p4_send(msg_type, proc_id, &m1, ln);
  146. }
  147.  
  148. receive_row(grid, row, proc_id)
  149. double grid[ROWS_PER_SUB+2][COLUMNS_PER_SUB+2];
  150. int row;
  151. int proc_id;
  152. {
  153.     struct r_boundary *m1;
  154.     int sender, mln, x, msg_type;
  155.     
  156.     msg_type = R_BOUNDARY;
  157.     sender   = proc_id;
  158.     m1 = NULL;
  159.     p4_recv(&msg_type,&sender, &m1, &mln);
  160.     for (x=1; x <= COLUMNS_PER_SUB; x++)
  161.     grid[row][x] = m1->row[x-1];
  162.     p4_msg_free(m1);
  163. }
  164.  
  165. receive_col(grid, col, proc_id)
  166. double grid[ROWS_PER_SUB+2][COLUMNS_PER_SUB+2];
  167. int col;
  168. int proc_id;
  169. {
  170.     struct c_boundary *m1;
  171.     int sender, mln, x, msg_type;
  172.     
  173.     msg_type = C_BOUNDARY;
  174.     sender = proc_id;
  175.     m1 = NULL;
  176.     p4_recv(&msg_type,&sender, &m1, &mln);
  177.     for (x=1;x <= ROWS_PER_SUB; x++)
  178.     grid[x][col] = m1->col[x-1];
  179.     p4_msg_free(m1);
  180. }
  181.  
  182.  
  183. send_answer(grid, master_id)
  184. double grid[ROWS_PER_SUB+2][COLUMNS_PER_SUB+2];
  185. int master_id;
  186. {
  187.     struct answer_rec rec1;
  188.     int i, j, ln, msg_type;
  189.     
  190.     msg_type = ANSWER;
  191.     for (i=1; i <= ROWS_PER_SUB; i++)
  192.     for (j=1; j <= COLUMNS_PER_SUB; j++)
  193.         rec1.subgrid[i-1][j-1] = grid[i][j];
  194.     ln = sizeof(struct answer_rec);
  195.     p4_dprintfl(5,"sending answer\n");
  196.  
  197.     p4_sendr(msg_type,master_id, &rec1, ln);
  198.  
  199.     p4_dprintfl(5,"sent answer\n");
  200. }
  201.  
  202. compute1_iter(current,next)
  203. double current[ROWS_PER_SUB+2][COLUMNS_PER_SUB+2];
  204. double next[ROWS_PER_SUB+2][COLUMNS_PER_SUB+2];
  205. {
  206.     int i, j;
  207.     
  208.     for (i=1; i <= (ROWS_PER_SUB); i++)
  209.     for (j=1; j <= COLUMNS_PER_SUB; j++)
  210.         next[i][j] = (current[i-1][j] + 
  211.               current[i+1][j] + 
  212.               current[i][j-1] + 
  213.               current[i][j+1]) / 4.0;
  214. }
  215.  
  216. printsubgrid(grid,r,c)
  217. double grid[ROWS_PER_SUB+2][COLUMNS_PER_SUB+2];
  218. int r,c;
  219. {
  220.     int i,j;
  221.  
  222.     for (i = 0; i < (r+2); i++)
  223.     for (j = 0; j < (c+2); j++)
  224.         printf("grid[%3d][%3d] = %10.5f\n",i,j,grid[i][j]);
  225. }
  226.